package com.yds.views;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

/**
 * 创建者：于德水
 * 创建时间：2021/12/8 0008 16:58
 * 创建描述：edittext ios
 * <p>
 * 必要属性：
 * style="@style/EditText.Compat"
 * android:theme="@style/YDSEditText"
 */
public class YDSEditText extends androidx.appcompat.widget.AppCompatEditText implements View.OnFocusChangeListener {
    private final Drawable drawableLeftIcon;
    private Drawable drawableRightIcon;
    private final int drawableRightIconVisible;

    private OnYDSEditTextFocusChangeListener mOnYDSEditTextFocusChangeListener;
    private OnYDSEditTextClearClickListener mOnYDSEditTextClearClickListener;

    public YDSEditText(@NonNull Context context) {
        this(context, null);
    }

    public YDSEditText(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    @SuppressLint("UseCompatLoadingForDrawables")
    public YDSEditText(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        @SuppressLint("Recycle") TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.YDSEditText);
        int drawableIconPadding = typedArray.getInt(R.styleable.YDSEditText_yds_drawableIconPadding, 12);
        setCompoundDrawablePadding(dpToPx(drawableIconPadding));
        //左侧图标 一般为搜索icon直接展示
        drawableLeftIcon = typedArray.getDrawable(R.styleable.YDSEditText_yds_drawableLeftIcon);
        if (null != drawableLeftIcon) {
            setCompoundDrawablesRelativeWithIntrinsicBounds(drawableLeftIcon, null, null, null);
        }
        //右侧图标 一般为清空icon 根据输入内容判断是否展示
        drawableRightIcon = typedArray.getDrawable(R.styleable.YDSEditText_yds_drawableRightIcon);
        drawableRightIcon = (null == drawableRightIcon) ? getResources().getDrawable(R.mipmap.icon_edittext_clear) : drawableRightIcon;
        drawableRightIconVisible = typedArray.getInt(R.styleable.YDSEditText_yds_drawableRightIconVisible, 2);
        //右侧图标常显
        if (drawableRightIconVisible == 0) {
            drawableRightIcon.setBounds(0, 0, drawableRightIcon.getIntrinsicWidth(), drawableRightIcon.getIntrinsicHeight());
            setCompoundDrawablesRelative(drawableLeftIcon, null, drawableRightIcon, null);
        }
        typedArray.recycle();
        setOnFocusChangeListener(this);
    }

    @Override
    protected void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) {
        super.onTextChanged(text, start, lengthBefore, lengthAfter);
        //右侧图标常显将不再受输入内容控制
        if (drawableRightIconVisible == 2) {
            if (null != drawableRightIcon) {
                if (TextUtils.isEmpty(text)) {
                    drawableRightIcon.setBounds(0, 0, 0, 0);
                } else {
                    drawableRightIcon.setBounds(0, 0, drawableRightIcon.getIntrinsicWidth(), drawableRightIcon.getIntrinsicHeight());
                }
                setCompoundDrawablesRelative(drawableLeftIcon, null, drawableRightIcon, null);
            }
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        //横向需处理getPaddingRight 防止点击位置错位
        if (event.getAction() == MotionEvent.ACTION_UP
                && event.getX() > getWidth() - getPaddingRight() - drawableRightIcon.getIntrinsicWidth() - 20
                && event.getX() < getWidth() - getPaddingRight() + 20
                && (int) event.getY() > (getHeight() / 2) - drawableRightIcon.getIntrinsicHeight() / 2 - 20
                && (int) event.getY() < getHeight() / 2 + drawableRightIcon.getIntrinsicHeight() / 2 + 20
        ) {

            if (null != getText()) {
                getText().clear();
                //清空按钮点击事件
                if (null != mOnYDSEditTextClearClickListener) {
                    mOnYDSEditTextClearClickListener.onClearClick();
                }
            }
        }
        performClick();
        return super.onTouchEvent(event);
    }

    @Override
    public boolean performClick() {
        return super.performClick();
    }

    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if (null != mOnYDSEditTextFocusChangeListener) {
            String content = "";
            if (null != getText()) {
                content = getText().toString().trim();
            }
            mOnYDSEditTextFocusChangeListener.onFocusChange(v, hasFocus, content);
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
    }


    /**
     * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
     */
    private int dpToPx(float dpValue) {
        final float scale = getContext().getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    /**
     * 设置焦点状态回调
     *
     * @param listener 焦点监听
     */
    public void setOnYDSEditTextFocusChangeListener(OnYDSEditTextFocusChangeListener listener) {
        this.mOnYDSEditTextFocusChangeListener = listener;
    }

    public interface OnYDSEditTextFocusChangeListener {
        void onFocusChange(View view, boolean hasFocus, String content);
    }

    /**
     * 设置焦点状态回调
     *
     * @param listener 焦点监听
     */
    public void setOnYDSEditTextClearClickListener(OnYDSEditTextClearClickListener listener) {
        this.mOnYDSEditTextClearClickListener = listener;
    }

    public interface OnYDSEditTextClearClickListener {
        void onClearClick();
    }
}
